package com.powernode.controller;

/**
 * Created with IntelliJ IDEA.
 * 系统操作日志管理控制器
 * @Author: DongGuoZhen
 * @Date: 2025/10/09/13:54
 * @Description:
 */

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysLog;
import com.powernode.model.Result;
import com.powernode.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "系统操作日志管理")
@RequestMapping("sys/log")
@RestController
public class SysLogController {


    @Autowired
    private SysLogService sysLogService;


    /**
     * 多条件分页查询系统日志
     * @param current 页码
     * @param size 每页显示数量
     * @param userId 用户标识
     * @param operation  操作
     * @return 分页数据
     */
    @ApiOperation("多条件分页查询系统日志")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String operation) {
        Page<SysLog> page = new Page<>(current,size);

        sysLogService.page(page,new LambdaQueryWrapper<SysLog>()
//                如果eq和like方法都存在，则优先写eq方法
                .eq(ObjectUtil.isNotNull(userId),SysLog::getUserId, userId)
                .like(StringUtils.hasText(operation),SysLog::getOperation, operation)
                .orderByDesc(SysLog::getCreateDate));
        return Result.success(page);
    }
}
